<template>
  <!-- Free Cash -->
  <el-dialog :visible.sync="isShow" v-if="isShow" title="Free Cash" width="60%" :before-close="handleClose" top="50px" :close-on-click-modal="false">
    <div class="border-box">
      <page-details ref="baseForm" :activity_type="9" :data="data"/>
      <el-form ref="activityForm" :model="formParams" :rules="ruleForm" label-position="right" label-width="95px">
        <div class="flex item-box" v-for="(item,index) in activityData" :key="index">
          <div class="title width25">
            {{ item.label }}
          </div>
          <div class="content width80">
            <!-- 新用户初始值 -->
            <div v-show="item.key=='new'" class="flex" style="height:50px">
              <el-form-item label="初始值Min:" label-width="90px">
                <el-input-number :min="0" :max="999999999"  v-model="formParams.other.newUserMin" controls-position="right" clearable placeholder="请输入新用户初始值Min"/>
              </el-form-item>
              <el-form-item label="初始值Max:" label-width="120px">
                <el-input-number :min="0" :max="999999999"  v-model="formParams.other.newUserMax" controls-position="right" clearable placeholder="请输入新用户初始值Max"/>
              </el-form-item>
            </div>
            <!-- 邀请非充值用户成长值 -->
            <div v-show="item.key=='grow'" class="flex" style="height:50px">
              <el-form-item label="成长值Min:" label-width="90px">
                <el-input-number :min="0" :max="999999999"  v-model="formParams.other.noRechargeMin" controls-position="right" clearable placeholder="邀请非充值用户成长值Min"/>
              </el-form-item>
              <el-form-item label="成长值Max:" label-width="120px">
                <el-input-number :min="0" :max="999999999"  v-model="formParams.other.noRechargeMax" controls-position="right" clearable placeholder="邀请非充值用户成长值Max"/>
              </el-form-item>
            </div>
            <!-- 邀请充值用户成长值 -->
            <div v-show="item.key=='recharge'" class="flex" style="height:50px">
              <el-form-item label="成长值Min:" label-width="90px">
                <el-input-number :min="0" :max="999999999"  v-model="formParams.other.rechargeMin" controls-position="right" clearable placeholder="请输入邀请充值用户成长值Min" />
              </el-form-item>
              <el-form-item label="成长值Max:" label-width="120px">
                <el-input-number :min="0" :max="999999999"  v-model="formParams.other.rechargeMax" controls-position="right" clearable placeholder="请输入邀请充值用户成长值Max" />
              </el-form-item>
            </div>
            <!-- 最大提现额度 -->
            <div v-show="item.key=='withdrawal'" class="flex" style="height:50px">
              <el-input-number :min="0" :max="999999999999"  v-model="formParams.other.withdraw" controls-position="right" clearable placeholder="请输入最大提现额度"/>
            </div>
            <!-- 邀请非充值用户成长值(展示) -->
            <div v-show="item.key=='show_grow'" class="flex" style="height:50px">
              <el-form-item label="成长值Min:" label-width="90px">
                <el-input-number :min="0" :max="999999999"  v-model="formParams.other.show_noRechargeMin" controls-position="right" clearable placeholder="邀请非充值用户成长值Min" />
              </el-form-item>
              <el-form-item label="成长值Max:" label-width="120px">
                <el-input-number :min="0" :max="999999999" v-model="formParams.other.show_noRechargeMax" controls-position="right" clearable placeholder="邀请非充值用户成长值Max" />
              </el-form-item>
            </div>
            <!-- 邀请充值用户成长值(展示) -->
            <div v-show="item.key=='show_recharge'" class="flex" style="height:50px">
              <el-form-item label="成长值Min:" label-width="90px">
                <el-input-number :min="0" :max="999999999" v-model="formParams.other.show_rechargeMin" controls-position="right" clearable placeholder="请输入邀请充值用户成长值Min" />
              </el-form-item>
              <el-form-item label="成长值Max:" label-width="120px">
                <el-input-number :min="0" :max="999999999" v-model="formParams.other.show_rechargeMax" controls-position="right" clearable placeholder="请输入邀请充值用户成长值Max" />
              </el-form-item>
            </div>
          </div>
        </div>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button size="medium" @click="handleClose">取 消</el-button>
      <el-button size="medium" type="primary" @click="handleConfirm('activityForm')">确 认</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {
  addActivity,
  editActivity
} from "@/api/activity";

import PageDetails from '../page-details.vue'
export default {
  components: {
    PageDetails
  },
  props: {
    show: {
      type: Boolean,
      default: false
    },
    data: {
      type: Object,
      default: function () {
        return {}
      }
    },
  },
  data () {
    return {
      isShow: false,
      activityData:[
        { label: '新用户初始值', key: 'new' },
        { label: '邀请非充值用户成长值', key: 'grow' },
        { label: '邀请充值用户成长值', key: 'recharge' },
        { label: '最大提现额度', key: 'withdrawal' },
        { label: '邀请非充值用户成长值(展示)', key: 'show_grow' },
        { label: '邀请充值用户成长值(展示)', key: 'show_recharge' },
      ],
      formParams: {
        other: { // 活动的条件
          newUserMin:'',
          newUserMax:'',
          noRechargeMin:'',
          noRechargeMax:'',
          rechargeMin:'',
          rechargeMax:'',
          show_rechargeMin:'',
          show_rechargeMax:'',
          show_noRechargeMin:'',
          show_noRechargeMax:'',
          withdraw:'',
        }, 
      },
      ruleForm: {},
    }
  },
  computed: {},
  watch: {
    show: function () {
      let that = this;
      that.isShow = that.show
      if (that.data) {
        that.formParams = JSON.parse(JSON.stringify(that.data));
        if(that.formParams.parameter){
          that.formParams.other = that.formParams.parameter
        }else{
          that.formParams = {
            other: {
              newUserMin:'',
              newUserMax:'',
              noRechargeMin:'',
              noRechargeMax:'',
              rechargeMin:'',
              rechargeMax:'',
              show_rechargeMin:'',
              show_rechargeMax:'',
              show_noRechargeMin:'',
              show_noRechargeMax:'',
              withdraw:'',
            },
          }
        }
      } else {
        that.formParams = {
          other: {
            newUserMin:'',
            newUserMax:'',
            noRechargeMin:'',
            noRechargeMax:'',
            rechargeMin:'',
            rechargeMax:'',
            show_rechargeMin:'',
            show_rechargeMax:'',
            show_noRechargeMin:'',
            show_noRechargeMax:'',
            withdraw:'',
          },
        }
      }
    },
  },
  mounted () {},
  methods: {
    // 确认
    handleConfirm (formvalue) {
      if(!this.$refs.baseForm.handleConfirm()){
        return false
      }
      let that = this,parameter = JSON.parse(JSON.stringify(that.formParams)),
      params = JSON.parse(JSON.stringify(this.$refs.baseForm.handleConfirm()));
      for (const key in parameter.other) {
        parameter.other[key]=that.$public.mathNumberMultiply(parameter.other[key],10000)
      }
      // 初始值
      if(parameter.other.newUserMin && parameter.other.newUserMin!=0 && parameter.other.newUserMax && parameter.other.newUserMax!=0 && parameter.other.newUserMin>parameter.other.newUserMax){
        return that.$message.error('请输入正确的用户初始值')
      }
      // 非充值用户成长值
      if(parameter.other.noRechargeMin && parameter.other.noRechargeMin!=0 && parameter.other.noRechargeMax && parameter.other.noRechargeMax!=0 && parameter.other.noRechargeMin>parameter.other.noRechargeMax){
        return that.$message.error('请输入正确的非充值用户成长值')
      }
      if(parameter.other.show_noRechargeMin && parameter.other.show_noRechargeMin!=0 && parameter.other.show_noRechargeMax && parameter.other.show_noRechargeMax!=0 && parameter.other.show_noRechargeMin>parameter.other.show_noRechargeMax){
        return that.$message.error('请输入正确的展示非充值用户成长值')
      }
      // 充值用户成长值
      if(parameter.other.rechargeMin && parameter.other.rechargeMin!=0 && parameter.other.rechargeMax && parameter.other.rechargeMax!=0 && parameter.other.rechargeMin>parameter.other.rechargeMax){
        return that.$message.error('请输入正确的充值用户成长值')
      }
      if(parameter.other.show_rechargeMin && parameter.other.show_rechargeMin!=0 && parameter.other.show_rechargeMax && parameter.other.show_rechargeMax!=0 && parameter.other.show_rechargeMin>parameter.other.show_rechargeMax){
        return that.$message.error('请输入正确的展示充值用户成长值')
      }
      params.activity_type=9
      params.other=JSON.stringify(parameter.other)
      that.$refs[formvalue].validate((valid) => {
        if (valid) {
          if(params.id){
            editActivity(params).then(function (res) {
              that.$message.success(res.msg)
              that.$emit('onHideDetails', true)
            }).catch((error) => {
              that.$message.error(error.msg)
            })
          }else{
            addActivity(params).then((res)=> {
              that.$message.success(res.msg)
              that.$emit('onHideDetails', true)
            }).catch((error) => {
              that.$message.error(error.msg)
            })
          }
        } else {
          return false;
        }
      });
    },
    // 取消
    handleClose () {
      this.$emit('onHideDetails', false)
    },
  }
}
</script>

<style scoped lang="scss">
.el-form-item{
  margin-bottom: 0;
}
.box-title {
  font-size: 17px;
  font-weight: 600;
}
.prooItem {
  margin: 5px;
  font-size: 16px;
  font-weight: 600;
  p{
    height: 24px;
  }
}
</style>